package javaweblearn.session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ListCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        List<Books> list = (List) session.getAttribute("list");
                 if(list==null || list.size()==0){
                         out.write("对不起，您还没有购买任何商品!!");
                         return;
                     }
                 //显示用户买过的商品
                 out.write("您买过如下商品:<br>");
                 for(Books book : list){
                         out.write(book.getName() + "<br/>");
                     }
             }

             public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
                doGet(request, response);
           }
    }
